package web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.DBCon;
import com.MD5;

import java.sql.*;
/**
 * Servlet implementation class ServletAddDepNum
 */
@WebServlet({ "/ServletAddDepNum", "/Manager/AddDepNum" })
public class ServletAddDepNum extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    //接收学院账号，和当前所在页码
	    request.setCharacterEncoding("utf-8");
	     //合并学院账号
	    String DepNum ="106512"+request.getParameter("DepNum");
	    String Pwd = request.getParameter("Pwd");
	    if(Pwd!=null) {
	    	Pwd=MD5.md5(Pwd);
	    }
	    String DepName = request.getParameter("DepName");
	    String DepConName = request.getParameter("DepConName");
	     //合并学院邮箱
	    String DepConEmail1=request.getParameter("DepConEmail1");
	    String DepConEmail2=request.getParameter("DepConEmail2");
	    String DepConEmail = DepConEmail1+DepConEmail2;
	    String pg=(String) request.getSession().getAttribute("pagenumber");
	    String btn=request.getParameter("btn");
	    
	    //返回学院账号和学院名称的查找情况
	    String DepNumFB="";
	    String DepNameFB="";
	    String FindDepNum="";
	    String FindDepName="";
	    ResultSet test1;
	    ResultSet test2;
	    
	    //接收需要修改的账号
	    String EditUserNum=(String)request.getSession().getAttribute("EditUserNum");
	    //接收需要修改的学院名称（这两个需要进行异常处理）
	    String EditDepName=(String)request.getSession().getAttribute("EditDepName");
	    String sql1 = "";
	    String sql2 = "";
	    //对按钮进行判断
	    if(btn.equals("返回")) {
		    //返回页面
			request.setAttribute("p",pg);
			//重定向
		    request.getRequestDispatcher("DepEdit.jsp").forward(request,response);
	    }else if(btn.equals("添加")){
			    try {
					test1=new DBCon().find("Select UserNum from user where UserNum='"+DepNum+"'");
					while(test1.next()) {
						FindDepNum=test1.getString("UserNum");	
						System.out.println("SADN"+FindDepNum);
					}
					if(FindDepNum==null || !FindDepNum.equals(DepNum)) {DepNumFB="不存在账号";}else {DepNumFB="存在账号";}
					
					test2=new DBCon().find("Select DepName from department where DepName='"+DepName+"'");
					while(test2.next()) {
						FindDepName=test2.getString("DepName");	
						System.out.println("SADN"+FindDepName);
					}
					if(FindDepName==null || !FindDepName.equals(DepName)) {DepNameFB="不存在学院";}else {DepNameFB="存在学院";}
					//当且仅当学院名称、学院账号不重复才执行插入数操作
					if(DepNumFB.equals("不存在账号")&&DepNameFB.equals("不存在学院")) {
					    	 //构造写入数据库的SQL
						     //新增学院表内容
						     sql1= "insert into department values('"+DepName+"','"+DepNum+"','"+DepConName+"','"+DepConEmail+"')";
						     //新增用户表内容
						     sql2= "insert into user values('"+DepNum+"','"+Pwd+"','学院联系人')";  
						     System.out.println("DepInsert sql1=" + sql1);
						     System.out.println("UserInsert sql2=" + sql2);
						    //连接数据库
								try {
									int ret1;
									int ret2;
									ret1 = new DBCon().query(sql1);
									ret2 = new DBCon().query(sql2);
							        System.out.println("ret1=" + ret1);
							        System.out.println("ret2=" + ret2);
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							    //返回页面
								//request.setAttribute("p",pg);
								//任一为空，直接返回新增界面
								request.setAttribute("p",pg);//返回之前所在页的参数					
								request.getRequestDispatcher("DepEdit.jsp").forward(request,response);			    
					}else {
						request.setAttribute("p",pg);//返回之前所在页的参数
						request.setAttribute("DepNumFB",DepNumFB);
						request.setAttribute("DepNameFB",DepNameFB);
						//返回提交失败前页面的值
						request.setAttribute("DepNum",DepNum);
						request.setAttribute("DepName",DepName);
						request.setAttribute("DepConName",DepConName);
						request.setAttribute("DepConEmail1",DepConEmail1);
						request.setAttribute("DepConEmail2",DepConEmail2);
						request.setAttribute("submitFB","提交失败");
						request.getRequestDispatcher("AddDep.jsp").forward(request,response);	
					}
				} catch (ClassNotFoundException | SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
	    	}else if(btn.equals("修改")) {
	    		 try {
						test1=new DBCon().find("Select UserNum from user where UserNum='"+DepNum+"'");
						while(test1.next()) {
							FindDepNum=test1.getString("UserNum");	
							System.out.println("SADN"+FindDepNum);
						}
						if(FindDepNum==null || !FindDepNum.equals(DepNum)) {DepNumFB="不存在账号";}else {DepNumFB="存在账号";}
						
						test2=new DBCon().find("Select DepName from department where DepName='"+DepName+"'");
						while(test2.next()) {
							FindDepName=test2.getString("DepName");	
							System.out.println("SADN"+FindDepName);
						}
						if(FindDepName==null || !FindDepName.equals(DepName)) {DepNameFB="不存在学院";}else {DepNameFB="存在学院";}
						
						if(DepNumFB.equals("存在账号") || DepNameFB.equals("存在学院")) {
							//判断修改前后账号是否有改变
							if(DepNum.equals(EditUserNum)) {
								//如果没改变，说明User账号不用
								DepNumFB="修改前后账号相同";
							}
							//判断修改前后账号名称是否有改变
							if(DepName.equals(EditDepName)) {
								//如果没改变，说明User账号不用
								DepNameFB="修改前后学院相同";
							}
						}
						
						//当且仅当学院名称、学院账号不重复才执行更新操作
						if(DepNumFB.equals("不存在账号")&&DepNameFB.equals("不存在学院") || DepNumFB.equals("修改前后账号相同")&& DepNameFB.equals("修改前后学院相同")
								|| DepNumFB.equals("修改前后账号相同")&& DepNameFB.equals("不存在学院") || DepNumFB.equals("不存在账号")&& DepNameFB.equals("修改前后学院相同")) {
						    	 //构造写入数据库的SQL
							     //更新用户表内容
							     sql1="UPDATE user SET UserNum='"+DepNum+"'"+" WHERE UserNum='"+EditUserNum+"'";
							     //更新学院表内容
							     sql2= "UPDATE department SET DepNum='"+DepNum+"',"+" DepName='"+DepName+"',"+" DepConName='"+DepConName+"',"+" DepConEmail='"+DepConEmail+"'"
							    		 +" Where DepNum='"+EditUserNum+"'";  
							     System.out.println("DepInsert sql1=" + sql1);
							     System.out.println("UserInsert sql2=" + sql2);
							    //连接数据库
									try {
										int ret1;
										int ret2;
										ret1 = new DBCon().query(sql1);
										ret2 = new DBCon().query(sql2);
								        System.out.println("ret1=" + ret1);
								        System.out.println("ret2=" + ret2);
									} catch (Exception e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								    //返回页面
									//request.setAttribute("p",pg);
									//更新完成
									request.setAttribute("p",pg);//返回之前所在页的参数					
									request.getRequestDispatcher("DepEdit.jsp").forward(request,response);
									System.out.println(DepNameFB+","+DepNumFB+","+"修改完成");
						}else {
							System.out.println("执行返回操作");
							request.setAttribute("p",pg);//返回之前所在页的参数
							request.setAttribute("DepNumFB",DepNumFB);
							request.setAttribute("DepNameFB",DepNameFB);
							//返回提交失败前页面的值
							request.setAttribute("DepNum",DepNum);
							request.setAttribute("DepName",DepName);
							request.setAttribute("DepConName",DepConName);
							request.setAttribute("DepConEmail1",DepConEmail1);
							request.setAttribute("DepConEmail2",DepConEmail2);
							request.setAttribute("submitFB","提交失败");
							request.getRequestDispatcher("DepInfoEdit.jsp").forward(request,response);	
						}
					} catch (ClassNotFoundException | SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
	    		
	    	}
	    }


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);

	}

}
